<?php

class Application_Helper implements Application_Interface_Singleton {

    /**
     * @var Application_Helper
     */
    static private $_instance;
    private $_configFile     = "etc/config.xml";
    private $_config         = null;
    private $_templateEngine = null;

    /**
    * @return Application_Helper
    */
    static public function getInstance() {
        if (!self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
   * Returns instance of template engine. It cannot be saved in 
   *
   * @return HTML_Template_Flexy
   */
    public function getTemplateEngine() {
        return $this->_templateEngine;
    }

    public function getConfig($section = null) {
        if ($section) {
            return $this->_config->{$section};
        }
        return $this->_config;
    }
    
    private function _initTemplateEngine() {
        $templateEngingeConfig = $this->getConfig('templateEngine');
        $templateOptions = &PEAR::getStaticProperty('HTML_Template_Flexy','options');
        $templateOptions = array(
            'templateDir'   => (string)$templateEngingeConfig->templateDir,
            'compileDir'    => (string)$templateEngingeConfig->compileDir,
            'forceCompile'  => (int)$templateEngingeConfig->forceCompile,
            'debug'         => (int)$templateEngingeConfig->debug,
            'locale'        => (string)$templateEngingeConfig->locale,
            'compiler'      => (string)$templateEngingeConfig->compiler,
        );
        $this->_templateEngine = new HTML_Template_Flexy();        
    }
    
    public function init() {
        $this->_readConfig();   
        $this->_initTemplateEngine();
        $this->_initDataManger();
        $this->_initLogger();
    }
    
    private function _readConfig() {
        if (!$this->_config) {
            $this->_config = SimpleXml_load_file($this->_configFile);
        }
    }
    
    private function _initDataManger() {
        epManager::instance()->setConfig((array)$this->getConfig('ezpdo'));
    }
    
    private function _initLogger() {
        Zend_Log::registerLogger(new Zend_Log_Adapter_File('log'.DIRECTORY_SEPARATOR.'application.log'));
    }
    
}

?>